<template>
  <div class="goods-comment">
      <!-- 测试单选 -->
    <!-- <XtxCheckBox v-model="isChecked"/> -->
     <div class="head">
      <div class="data">
        <p>
          <span>{{totalCounts.salesCount}}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{totalCounts.praisePercent}}</span
          ><span>好评率</span>
        </p>
        <p>
          <span>{{totalCounts.evaluateCount}}</span
          ><span>条评价</span>
        </p>
        <p>
          <span>{{totalCounts.hasPictureCount}}</span>
          <span>条有图评价</span>
        </p>
      </div>
    </div>
     <!-- 排序 -->
    <div class="sort">
      <span>排序：</span>
      <a @click="sortList(null)" href="javascript:;" :class="{ active: reqParams.sortField === null }">默认</a>
      <a @click="sortList('praiseCount')" href="javascript:;" :class="{ active: reqParams.currSort === 'praiseCount' }"
        >最热</a
      >
      <a @click="sortList('createTime')" href="javascript:;" :class="{ active: reqParams.sortField === 'createTime' }"
        >最新</a
      >
      <XtxCheckBox style="margin-left: 50px" v-model="reqParams.hasPicture">是否有图</XtxCheckBox>
    </div>
    <!-- 列表 -->
    <div class="list">
      <div class="item" v-for="item in commentList" :key="item">
        <div class="user">
          <img
            :src="item.member.avatar"
            alt=""
          />
          <span>{{item.member.nickname}}</span>
        </div>
        <div class="body">
          <div class="score">
            <i class="iconfont icon-wjx01" v-for="score in item.score" :key="score"></i>
            <i class="iconfont icon-wjx02" v-for="score in 5-item.score" :key="score"></i>
            <span class="attr">颜色：黑色 尺码：M</span>
          </div>
          <div class="text">
           {{item.content}}
          </div>
          <GoodsCommentImage :pictures="item.pictures"/>
          <div class="time">
            <span>{{item.createTime}}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{item.praiseCount}}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { ref, onMounted, watch } from 'vue'
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
import GoodsCommentImage from './goods-comment-image.vue'
const useTotal = () => {
  const totalCounts = ref({})
  const route = useRoute()
  const getfindComment = async () => {
    const { result } = await findCommentInfoByGoods(route.params.id)
    totalCounts.value = result
    // console.log(result)
  }
  onMounted(() => {
    getfindComment()
  })
  return { totalCounts }
}

const useList = () => {
  const commentList = ref([])
  const route = useRoute()
  const reqParams = ref({
    page: 1,
    pageSize: 10,
    hasPicture: false, // 评论数据是否有图
    sortField: null // 排序: praiseCount -> 最热 createTime -> 最新
  })
  const getCommentList = async () => {
    const { result: { items } } = await findCommentListByGoods(route.params.id, reqParams.value)
    // console.log(items)
    commentList.value = items
  }
  watch(() => reqParams.value.hasPicture, () => {
    // 刷新页面数据
    getCommentList()
  })
  const sortList = (currSort) => {
    // 1. 选中高亮
    reqParams.value.sortField = currSort
    // 刷新页面数据
    getCommentList()
  }
  onMounted(() => {
    getCommentList()
  })
  return { commentList, sortList, reqParams }
}
export default {
  name: 'GoodsComment',
  components: {
    GoodsCommentImage
  },
  setup () {
    const { totalCounts } = useTotal()
    const { commentList, sortList, reqParams } = useList()
    return { totalCounts, commentList, sortList, reqParams }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      flex:1;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
.list {
  padding: 0 20px;
  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;
    .user {
      width: 160px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }
      span {
        padding-left: 10px;
        color: #666;
      }
    }
    .body {
      flex: 1;
      .score {
        line-height: 40px;
        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }
        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }
    .text {
      color: #666;
      line-height: 24px;
    }
    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}
</style>
